package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Vaccine;
import com.example.entity.vo.vaccineVO;
import com.example.service.IVaccineService;
import com.example.service.IVaccineTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 疫苗 前端控制器
 * </p>
 *
 * @author wyx
 * @since 2025-03-19
 */
@CrossOrigin//跨域访问
@Slf4j
@RestController
@Api(tags = "疫苗管理模块")
@RequestMapping("/vaccine")
public class VaccineController {
    @Autowired
    IVaccineService vaccineService;
    @Autowired
    IVaccineTypeService vaccineTypeService;

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name, Integer status) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        Page<Vaccine> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Vaccine> queryWrapper = new LambdaQueryWrapper<>();
        if (status != null) {
            queryWrapper.eq(Vaccine::getStatus, status);
        }
        if (name != null) {
            queryWrapper.like(Vaccine::getName, name);
        }

        // 查询疫苗数据
        Page<Vaccine> vaccinePage = vaccineService.page(pageInfo, queryWrapper);

        // 将Vaccine对象转换为VaccineVO对象，并填充type字段
        IPage<vaccineVO> vaccineVOPage = vaccinePage.convert(vaccine -> {
            vaccineVO vaccineVO = new vaccineVO();
            BeanUtils.copyProperties(vaccine, vaccineVO); // 复制属性
            vaccineVO.setType(vaccineTypeService.getById(vaccine.getTypeId()).getName()); // 根据实际逻辑填充type字段
            return vaccineVO;
        });

        return R.success(vaccineVOPage);
    }

    // 新增疫苗
    @ApiOperation(value = "新增疫苗")
    @PostMapping("/add")
    public R add(Vaccine vaccine) {
        log.info("新增疫苗：{}", vaccine);
        vaccineService.save(vaccine);
        return R.success("新增疫苗成功");
    }

    @ApiOperation(value = "删除疫苗")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        log.info("删除疫苗：{}", id);
        vaccineService.removeById(id);
        return R.success("删除疫苗成功");
    }

    @ApiOperation(value = "修改疫苗")
    @PutMapping("/update")
    public R update(Vaccine vaccine) {
        log.info("修改疫苗：{}", vaccine);
        vaccineService.updateById(vaccine);
        return R.success("修改疫苗成功");
    }

    @ApiOperation(value = "更新疫苗状态")
    @PutMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Integer id, Integer status) {
        log.info("更新疫苗状态：{}", status);
        Vaccine vaccine = new Vaccine();
        vaccine.setId(id);
        vaccine.setStatus(status);
        vaccineService.updateById(vaccine);
        return R.success("更新疫苗状态成功");
    }
}
